﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.WinForms.Controls;
using SMS.WinForms;
using SMS.Console.Controls;
using System.Xml;
using SMS.Core;

namespace SMS.Console
{
    public partial class ConfigWizardForm : SMS.WinForms.WizardForm
    {
        XmlDocument m_commDoc;
        XmlDocument m_serviceDoc;
        XmlDocument m_consoleDoc;
        public ConfigWizardForm()
        {
            InitializeComponent();
        }

        private void CreatePage()
        {
            m_commDoc = new XmlDocument();
            m_serviceDoc = new XmlDocument();
            m_consoleDoc = new XmlDocument();

            m_commDoc.Load(CommConfig.SMSCommConfig);
            m_serviceDoc.Load(CommConfig.SMSServiceConfig);
            m_consoleDoc.Load(CommConfig.SMSConsoleConfig);

            //串口参数配置
            this.Pages.Add("commSetting", new CommConfigPage() { Document = m_commDoc });
            //短信数据库配置
            this.Pages.Add("database", new DatabaseConfigPage() { Document = m_serviceDoc });           
            //短信服务配置
            this.Pages.Add("smsService", new WebSiteConfigPage() { ServiceDocument = m_serviceDoc, ConsoleDocument = m_consoleDoc });
           
            //完成
            WizardPageSettings settings = new WizardPageSettings();
            settings.NextButtonSettings.Text = "完成(&F)";
            this.Pages.Add("finish", new FinishConfigPage(), settings);
        }

        public string ConfigFilePath { get; private set; }

        private void ConfigWizardForm_Load(object sender, EventArgs e)
        {
            CreatePage();
        }

        private void ConfigWizardForm_Navigated(object sender, SMS.WinForms.WizardFormNavigationEventArgs e)
        {
            if (e.CurrentPage is FinishConfigPage && e.NavigationType== SMS.WinForms.WizardFormNavigationTypes.Next)
            {
                try
                {
                    m_commDoc.Save(CommConfig.SMSCommConfig);
                    m_serviceDoc.Save(CommConfig.SMSServiceConfig);
                    m_consoleDoc.Save(CommConfig.SMSConsoleConfig);
                }
                catch (Exception ex)
                {
                    ErrorBox.Show(ex);
                    throw;
                }
            }
        }
    }
}
